#ifndef EVENTFACTORY_H
#define EVENTFACTORY_H

#include "../global.h"
#include "../log.h"
#include "../memorypool.h"
#include "event.h"
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>

class EventFactory
{
public:
    typedef boost::shared_ptr< Event > EventPtr;

public:
    EventFactory();
    ~EventFactory();

    template< typename ET >
    EventPtr create()
    {
        void *p = m_mp.alloc(sizeof(ET) + sizeof(unsigned long));

        if(!p){
            return EventPtr();
        }

        unsigned long *l = (unsigned long *)p;
         *l = sizeof(ET) + sizeof(unsigned long);

        EventPtr _new
                (
                    new(p + sizeof(unsigned long)) ET(),
                    boost::bind
                    (
                        &EventFactory::destroy,
                        this,
                        _1
                    )
                );

        return _new;
    }

protected:
    void destroy(Event *p);

protected:
    MemoryPool m_mp;
};

#endif // EVENTFACTORY_H
